﻿using STMS.BLL;
using STMS.Models.DModels;
using STMS.WSTMSystem.FModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }
        UserBLL userBLL = new UserBLL();
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string uName = txtUName.Text.Trim();
            string uPwd = txtUPwd.Text.Trim();
            if (CheckLogin(uName, uPwd) == false) return;
            UserInfo user = new UserInfo()
            {
                UserName = uName,
                USerPwd = uPwd
            };
            int userId = userBLL.LoginSystem(user);
            if(userId>0)
            {
                FrmMain fMain = new FrmMain();
               // fMain.WindowState = FormWindowState.Maximized;
                fMain.Tag = new LoginData()
                {
                    UserId = userId,
                    UserName =uName,
                    fLogin=this
                };
                this.Hide();
                fMain.Show();
            }
            else
            {
                MsgBoxHelper.MsgErrorShow("登录提示", "账号或密码错误，请检查账号密码信息！");
                return;
            }

          
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            txtUName.Clear();
            txtUPwd.Clear();
          
        }

        private bool CheckLogin(string uName,string uPwd)
        {
            if(string.IsNullOrEmpty(uName))
            {
                MsgBoxHelper.MsgErrorShow("登录提示", "请输入账号！");
                txtUName.Focus();
                return false;
            }
            if(string.IsNullOrEmpty(uPwd))
            {
                MsgBoxHelper.MsgErrorShow("登录提示", "请输入密码！");
                txtUPwd.Focus();
                return false;
            }
            return true;
        }



        private void FrmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MsgBoxHelper.MsgBoxConfirm("退出系统", "你确定要退出系统吗？") == DialogResult.Yes)
            {
                Application.ExitThread();
            }
            else
                e.Cancel = true;
        }
    }
}
